package team2.arsystem.DomainObjects;

import java.util.ArrayList;
import java.util.Date;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import team2.arsystem.Controller.AccountController;
import team2.arsystem.Controller.TransactionController;
import team2.arsystem.Database.DBMediator;
import team2.arsystem.Utilities.CommandConstants;
import team2.arsystem.Utilities.Validate;
import android.util.Log;

/*This class will handle requests related to Debit Transaction only. This is again a Singleton.*/
public class DebitTransaction extends Transaction {
	
	private static DebitTransaction debitObject = new DebitTransaction();
	
	public static DebitTransaction getDebitTransactionObject()
	{
		return debitObject;
	}
	
	/*Will be used if we have the functionality to retry debit transaction in case of a failure.*/
	private boolean setAmount(float amt)
	{
		amount=amt;
		return true;
	}
	
	private double getAmount()
	{
		return amount;
	}
	
	/*TODO : Add more validation*/
	public boolean addDebitTransaction(String debitTxDescription, double debitTxAmount, boolean divideAmongMembers, Date transactionDate, Date reminderDate,String selectedMembers)
	{
		boolean addDebitTxSuccess = false;
		try
		{
			if(Validate.validateDescription(debitTxDescription))
			{

				if(addDebitTx(debitTxDescription, debitTxAmount, divideAmongMembers, transactionDate, reminderDate, selectedMembers))
				{
					Log.i("Status", "Success in adding the debit transaction");
					addDebitTxSuccess = true;
				}
				else
				{
					Log.i("Status" , "Failure in adding the debit transaction");
					addDebitTxSuccess = false;
				}
			}
			else
			{
				Log.i("Status" , "Invalid description");
				addDebitTxSuccess = false;
			}

			return addDebitTxSuccess;
		}
		
		catch(Exception e)
		{
		  e.printStackTrace();
		  return false;
		}
		
	}
	
	/*Get all the debit transactions with the reminder date set to the present day or any date following the present day.*/	
	public ArrayList<ArrayList<String>> getDebitReminders(String userName)
	{
		ArrayList<ArrayList<String>> debitRem;
		try
		{
			Date presentDay = new Date();
			debitRem = fetchDebitReminders(userName,presentDay);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			debitRem = null;
		
			
		}
		
		return debitRem;
	}
	
	
	/*helper method to add the debit transaction in the database*/
	private boolean addDebitTx(String debitTxDescription, double debitTxAmount, boolean divideAmongMembers, Date transactionDate, Date reminderDate,String selectedMembers) throws Exception
	{
		try {
			
			boolean addDebitTxSuccess = false;
			
		    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("command",CommandConstants.addDebitTx));
			nameValuePairs.add(new BasicNameValuePair("UserName", TransactionController.getTransactionController().getUserName()));
			nameValuePairs.add(new BasicNameValuePair("DebitDesc",debitTxDescription));
			nameValuePairs.add(new BasicNameValuePair("DebitAmount",Double.toString(debitTxAmount)));
			nameValuePairs.add(new BasicNameValuePair("divideAmongMembers",Boolean.toString(divideAmongMembers)));
			nameValuePairs.add(new BasicNameValuePair("transactionDate",transactionDate.getYear()+"-"+transactionDate.getMonth()+"-"+transactionDate.getDate()));
			nameValuePairs.add(new BasicNameValuePair("reminderDate",reminderDate.getYear()+"-"+reminderDate.getMonth()+"-"+reminderDate.getDate()));
			nameValuePairs.add(new BasicNameValuePair("selectedMembers",selectedMembers));
			
			Log.i("Tx date",transactionDate.getDate()+" "+transactionDate.getMonth()+" "+transactionDate.getYear());
			Log.i("rem date",reminderDate.getDate()+" "+reminderDate.getMonth()+" "+reminderDate.getYear());
			DBMediator.postHttpCommand(nameValuePairs);
		    
		    JSONObject response = DBMediator.getResponseFromServerAsObject();
		   
			if((response.getBoolean("DebitTransactionAdded"))==true)
			{
				addDebitTxSuccess=true;
				Log.i("Debit Transaction information added","SUCCESS");
			}
			
		 
		    return addDebitTxSuccess;
	    	
		}
		catch (Exception e) 
		{
			Log.i("errorrrr",e.getMessage());
			e.printStackTrace();
			return false;
		}
	}
	
	/*helper method for getDebitReminders()*/
	private ArrayList<ArrayList<String>> fetchDebitReminders(String userName,Date presentDay)
	{
		ArrayList<ArrayList<String>> debitRem = new ArrayList<ArrayList<String>>();
		try
		{
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("command",CommandConstants.getDebitReminders));
			nameValuePairs.add(new BasicNameValuePair("UserName", userName));
			nameValuePairs.add(new BasicNameValuePair("Date",(presentDay.getYear()+1900)+"-"+(presentDay.getMonth()+1)+"-"+presentDay.getDate()));
					
			Log.i("Current date",(presentDay.getYear()+1900)+"-"+(presentDay.getMonth()+1)+"-"+presentDay.getDate());		
			DBMediator.postHttpCommand(nameValuePairs);
		    
			JSONArray response = DBMediator.getResponseFromServerAsArray();
						
			 ArrayList<String> descriptions = new ArrayList<String>();
	    	    ArrayList<String> debitTxDate = new ArrayList<String>();
	    	    ArrayList<String> debitRemDate = new ArrayList<String>();
	    	    ArrayList<String> amount = new ArrayList<String>();
	    	    ArrayList<String> membersList = new ArrayList<String>();
	    	    			    
	    	    int j=1;
	    	    for(int i=0;i<response.length();i=i+6)
	    	    {
	    	    descriptions.add(response.getJSONObject(i).getString("Desc "+j));
				String currency = response.getJSONObject(i+2).getString("Currency "+j);
				String amountVal = response.getJSONObject(i+1).getString("Amt "+j);
				amountVal = amountVal + " "+getCurrency(currency);
				amount.add(amountVal);
				debitTxDate.add(response.getJSONObject(i+3).getString("BillDate "+j));
				debitRemDate.add(response.getJSONObject(i+4).getString("RemDate "+j));
			    membersList.add(response.getJSONObject(i+5).getString("members "+j));
			    j++;
		  		}
	    	    
	    	    for(int i=0;i<descriptions.size();i++)
	    	    {
	    	    	Log.i("Record "+i,descriptions.get(i) + debitTxDate.get(i)+debitRemDate.get(i)+amount.get(i)+membersList.get(i));
	    	    }
	    	
	    	 
	    	    debitRem.add(descriptions);
	    	    debitRem.add(debitTxDate);
	    	    debitRem.add(debitRemDate);
	    	    debitRem.add(amount);
	    	    debitRem.add(membersList);
			    
		     
		    		
		}
		catch(Exception e)
		{
			e.printStackTrace();
			debitRem = null;
			
		}
		
		return debitRem;
	}

	private String getCurrency(String currency)
	{
		String currencyMode = "";
		if(currency.equals("US Dollars"))
		  currencyMode = "USD";
		
		if(currency.equals("Canadian Dollars"))
			  currencyMode = "CAD";
			
		if(currency.equals("Rupees"))
			  currencyMode = "Rs";
			
		if(currency.equals("Euros"))
			  currencyMode = "EUR";
			
		return currencyMode;
		 	
	}
}
